<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\api\housing;

use think\App;
use app\controller\property\Base;
use app\validate\housing\HousingResourceValidate;
use app\common\repositories\housing\HousingMakeRepository;
use app\common\repositories\housing\HousingResourceRepository;

class HousingResource extends Base
{
    protected $repository;

    public function __construct(App $app, HousingResourceRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
    }

    /**
     * 获取房源列表
     * @return mixed
     *
     * @author Amos
     * 2024-06-03 19:01
     */
    public function resourceList()
    {
        $where = $this->request->param([
            'cate_id' => '',
            'label_id' => '',
            'longitude' => '',
            'latitude' => '',
            'keywords' => ''
        ]);
        [$page, $limit] = $this->getPage();
        $res = $this->repository->getApiList($where, $page, $limit, $this->request->propertyId);
        return app('api_return')->success($res);
    }

    /**
     * 获取房源详情
     * @param int $id
     * @return string|\think\response\Json|\think\response\View
     *
     * @author Amos
     * 2024-06-12 15:57
     */
    public function detail(int $id)
    {
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }
        $info = $this->repository->getApiDetail($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        return app('api_return')->success($info);
    }


    /**
     * 预约看房
     * @param int $id
     * @return string|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-12 16:16
     */
    public function makeAdd(int $id)
    {
        $param = $this->request->param([
            'hope_date' => '',
            'hope_time' => '',
            'remark' => '',
        ]);
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }

        validate(HousingResourceValidate::class)->scene('makeAdd')->check($param);

        /** @var HousingMakeRepository $housingMakeRepository */
        $housingMakeRepository = app()->make(HousingMakeRepository::class);

        $makeNum = $housingMakeRepository->getSearch([])
            ->where('user_id', $this->request->userId())
            ->where('status', 'in', ['1,2,3,4'])
            ->where('housing_id', $id)
            ->count();
        if ($makeNum > 0) {
            return $this->error('己预约');
        }

        $param['status'] = 1; ## 待确认
        $param['housing_id'] = $id;
        $param['user_id'] = $this->request->userId();
        $housingMakeRepository->addInfo($param, $this->request->propertyId, $this->request->userId());
        return app('api_return')->success([], '预约成功');

    }


}